﻿var Util = function () {
    var isChrome = false;
	var isSafari = false;
    var isIE = false;
	var isIE9 = false;
	var isFireFox = false;
	var isOpera = false;
    IsChrome(); //Detectamos si es Chrome
    IsIE();
	IsIE9();
	IsSafari();
	IsFireFox();
	IsOpera();
    // Metodos Privados
    // Remove the spaces from the entered and generated code
    function removeSpaces(string) {
        return string.split(' ').join('');
    }
    function IsChrome() {
        if (navigator.appName == "Netscape") {
            if (navigator.userAgent.indexOf("Chrome") > 0 && navigator.vendor == "Google Inc.") {
                isChrome = true;
                return;
            }
        }
        isChrome = false;
    }
    function IsIE() {
        if (navigator.appName == "Microsoft Internet Explorer") {
            isIE = true;
            return;
        }
        isIE = false;
    }
	function IsIE9() {
        if (navigator.appName == "Microsoft Internet Explorer" && navigator.appVersion.indexOf("MSIE 9.0") > 0) {
            isIE9 = true;
            return;
        }
        isIE9 = false;
    }
	function IsSafari() {
        if (navigator.appName == "Netscape") {
            if (navigator.userAgent.indexOf("Safari") > 0 && navigator.vendor == "Apple Computer, Inc.") {
                isSafari = true;
                return;
            }
        }
        isSafari = false;
    }
	function IsFireFox() {
        if (navigator.appName == "Netscape") {
            if (navigator.userAgent.indexOf("Firefox") > 0) {
                isFireFox = true;
                return;
            }
        }
        isFireFox = false;
    }
	function IsOpera() {
        if (navigator.appName == "Opera") {
                isOpera = true;
                return;
        }
        isOpera = false;
    }

    // Metodos publicos y Propiedades
    return {
        DrawCaptcha: function (lblCaptcha) {
            var a = Math.ceil(Math.random() * 10) + '';
            var b = Math.ceil(Math.random() * 10) + '';
            var c = Math.ceil(Math.random() * 10) + '';
            var d = Math.ceil(Math.random() * 10) + '';
            var e = Math.ceil(Math.random() * 10) + '';
            var f = Math.ceil(Math.random() * 10) + '';
            var g = Math.ceil(Math.random() * 10) + '';
            var code = a + ' ' + b + ' ' + ' ' + c + ' ' + d + ' ' + e + ' ' + f + ' ' + g;
            lblCaptcha.innerHTML = code;
        },
        // Validate the Entered input aganist the generated security code function   
        ValidCaptcha: function (lblCaptcha, txtInput) {
            var str1 = removeSpaces(lblCaptcha.innerHTML);
            var str2 = removeSpaces(txtInput.value);
            if (str1 == str2) return true;
            return false;
        },
        FormatNumber: function (num, prefix) {
            prefix = prefix || '';
            num += '';
            var splitStr = num.split('.');
            var splitLeft = splitStr[0];
            var splitRight = splitStr.length > 1 ? '.' + splitStr[1] : '';
            var regx = /(\d+)(\d{3})/;
            while (regx.test(splitLeft)) {
                splitLeft = splitLeft.replace(regx, '$1' + ',' + '$2');
            }
            return prefix + splitLeft + splitRight;
        },
        UnformatNumber: function (num) {
            return num.replace(/([^0-9\.\-])/g, '') * 1;
        },
        // Funcion que te permite recuperar variables de la url
        GetVar: function (nomv) {
            var url = document.location.href
            if (url.indexOf(nomv) != -1) {
                posiBeg = url.indexOf("=", url.indexOf(nomv))
                barre = url.indexOf("&", posiBeg)
                posiEnd = (barre != -1 ? barre : url.length)
                return url.substring(posiBeg + 1, posiEnd)
            } else {
                //informa de la ausencia del parametro  
                return 'Parámetro no encontrado.'
            }
        },
        ReloadPage: function () {
            window.location.reload();
        },
        CreaElementUp: function () {
            //div = document.getElementById(element);
            var newlink = document.createElement('a');
            newlink.setAttribute('id', 'scroll-up');
            newlink.setAttribute('onclick', "$('body,html').animate({scrollTop:0},800);")
            newlink.setAttribute('title', 'Subir');
            document.body.insertBefore(newlink, document.body.firstChild);
        },
        SetScrollWindow: function () {
            window.onscroll = function () {
                if (window.pageYOffset >= 150) {
                    $('#scroll-up').fadeIn(); //$get('scroll-up').style.display = "block";
                } else {
                    $('#scroll-up').fadeOut(); //$get('scroll-up').style.display = "none";
//                    $('#scroll-up').fadeOut('slow', function () {
//                          $get('scroll-up').style.display = "none";
//                    });
                }
            }
        },
        Exists: function (o) {
            if (typeof (o) != "undefined")
                return true;
            return false;
        },
        EncodeURI: function (uri) {
            return encodeURI(uri);
        },
        DecodeURI: function (uri) {
            return decodeURI(uri);
        },
        ConvertNumeber: function (num) {
            var conv = Number(num);
            if (conv == NaN)
                conv = 0;
            return conv;
        },
        IsNumeric: function (num) {
            var r = isNaN(num);
            return !r;
        },
        IsChrome: isChrome,
        IsIE: isIE,
		IsIE9: isIE9,
		IsSafari: isSafari,
		IsFireFox: isFireFox,
		IsOpera: isOpera,
        ScreenWidth: screen.width, //Tamaño total de la pantalla
        ScreenHeight: screen.height,
        ScreenAvailHeight: screen.availHeight, //Tamaño disponible por barras que tiene el explorador
        ScreenAvailWidth: screen.availWidth
    }
} ();